package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.callhistory.CallHistoryCallDetail;
import com.ucredit.babel.common.model.qcloud.QCloudAntiFraud;
import com.ucredit.babel.common.model.qcloud.QCloudAntiFraudConsumptionRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 腾讯云反欺诈消费服务(v2)
 *
 * @author renzhanzheng
 */

public class QcloudAntiFraudConsumptionParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 腾讯云反欺诈消费服务(v2)
     *
     * @param model
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        QCloudAntiFraudConsumptionRecord obj = (QCloudAntiFraudConsumptionRecord) model;
        QCloudAntiFraud antiFraud = obj.getAntiFraud();
        JSONObject info = antiFraud.getData() == null ? new JSONObject() : JSON.parseObject(antiFraud.getData());
        JSONArray infoArray = info.getJSONArray("riskInfo") == null ? new JSONArray() : info.getJSONArray("riskInfo");

        StringBuilder sb = getBaseInfo(obj, successful);

        //业务侧错误码
        sb.append(info.getString("codeDesc") == null ? "" : info.getString("codeDesc")).append(CRT001);
        //公共错误码
        sb.append(info.getString("code") == null ? "" : info.getString("code")).append(CRT001);
        //信息
        sb.append(info.getString("message") == null ? "" : info.getString("message")).append(CRT001);
        //该条记录能否查到
        sb.append(info.getString("found") == null ? "" : info.getString("found")).append(CRT001);
        //该条记录中的身份证能否查到
        sb.append(info.getString("idFound") == null ? "" : info.getString("idFound")).append(CRT001);
        //用户IP
        sb.append(info.getString("userIp") == null ? "" : info.getString("userIp")).append(CRT001);
        //风险分
        sb.append(info.getString("riskScore") == null ? "" : info.getString("riskScore")).append(CRT001);
        //其他模型详值
        sb.append(info.getString("otherModelScores") == null ? "" : info.getString("otherModelScores")).append(CRT001);

        StringBuilder detailSb = new StringBuilder(500);
        StringBuilder txSb = getBaseInfo(obj);
        int i = 0;
        for (Object value : infoArray) {
            JSONObject v = (JSONObject) value;
            i++;
            detailSb.delete(0, detailSb.length());
            //序号
            detailSb.append(i).append(CRT001);
            //风险详情值
            detailSb.append(v.getString("riskCodeValue") == null ? "": v.getString("riskCodeValue")).append(CRT001);
            //风险码
            detailSb.append(v.getString("riskCode") == null ? "": v.getString("riskCode")).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + detailSb.toString() + txSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        if (i == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tengxu_fraud_inc";
    }

    @Override
    public String getType() {
        return RecordType.QCLOUD_ANTIFRAUD_CONSUMPTION_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "腾讯云反欺诈消费服务(v2)";
    }
}
